<?php
/**
 * Description of PlayerAge
 *
 * @author Laszlo.Pokoradi
 */
class PlayerAge {
  const ISYOUTH_XPATH = "./@youth";
  const AGEYEARS_XPATH = "./years[1]";
  const AGEMONTHS_XPATH = "./months[1]";
  const AGEDAYS_XPATH = "./days[1]";

  private $isYouth;
  private $ageYears;
  private $ageMonths;
  private $ageDays;

  private $db;

  public function __construct($database) {
    $this->db = $database;

    $this->isYouth = true;
    $this->ageYears = 0;
    $this->ageMonths = 0;
    $this->ageDays = 0;
  }

  public function loadFromNode(DOMElement $docNode) {
    $doc = $docNode->ownerDocument;

    $xPath = new DOMXPath($doc);

    $qResult = $xPath->query(PlayerAge::ISYOUTH_XPATH,$docNode);
    $this->isYouth = $qResult->item(0)->nodeValue;

    $qResult = $xPath->query(PlayerAge::AGEYEARS_XPATH,$docNode);
    $this->ageYears = $qResult->item(0)->nodeValue;

    $qResult = $xPath->query(PlayerAge::AGEMONTHS_XPATH,$docNode);
    $this->ageMonths = $qResult->item(0)->nodeValue;

    $qResult = $xPath->query(PlayerAge::AGEDAYS_XPATH,$docNode);
    $this->ageDays = $qResult->item(0)->nodeValue;
  }

  public function toXHTML() {
    $xhtml = "{$this->ageYears} y, {$this->ageMonths} m, {$this->ageDays} d";

    return $xhtml;
  }

  public function insertNewToDB($playerId) {
    $this->db->set('playerId',$playerId);
    $this->db->set('isYouth',$this->isYouth);
    $this->db->set('ageYears',$this->ageYears);
    $this->db->set('ageMonths',$this->ageMonths);
    $this->db->set('ageDays',$this->ageDays);
    $query = $this->db->insert('playerage');

    if (!$query) {
      log_message('error', 'Error during playerage inserttion!');
      return false;
    }

    return $this->db->insert_id();
  }

  public function amendOldInDB($ageId) {
    $this->db->where('id',$ageId);
    $query = $this->db->get('playerage');

    if (!$query) {
      log_message('error','Error during playername amendment!');
    } else {
      $tRow = $query->row();
      $oldId = $tRow->id;
      $playerId = $tRow->playerId;

      if ($tRow->isYouth!=$this->isYouth
          || $tRow->ageYears!=$this->ageYears
          || $tRow->ageMonths!=$this->ageMonths) {
        $newId = $this->insertNewToDB($playerId);

        $this->db->set('playerId', $playerId);
        $this->db->set('table','playerage');
        $this->db->set('oldId',$oldId);
        $this->db->set('newId',$newId);
        $this->db->set('insertTime',date('Y-m-d H:i:s'));
        $query = $this->db->insert('playerhistory');

        if (!$query) {
          log_message('error','Playerage amend was unsuccesful!');
        } else {
          return $newId;
        }
      }
    }
    return $oldId;
  }


  public function loadFromDB($ageId) {
    $this->db->where('id',$ageId);
    $query = $this->db->get('playerage');

    if (!$query || $query->num_rows()==0) {
      log_message('error', 'PlayerAge ('.$ageId.') not found in the database!');
    } else {
      $result = $query->row();
      $this->isYouth = $result->isYouth;
      $this->ageYears = $result->ageYears;
      $this->ageMonths = $result->ageMonths;
      $this->ageDays= $result->ageDays;
    }
  }
}
?>
